﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CGM
{
	public class Texture2DDataItem : DataItem
	{
		/////////////////////////////////////////////////////////////////////////////
		// public methods

		public Texture2DDataItem()
			: base("2D Texture Files", "tex2d")
		{
			//// create and set up the picture box
			//m_PictureBox = new PictureBox();

			//m_PictureBox.BackgroundImage = Properties.Resources.Background;
			//m_PictureBox.Size = new Size(512, 512);
			//m_PictureBox.SizeMode = PictureBoxSizeMode.AutoSize;

			//ControlPanel.Controls.Add(m_PictureBox);

			//// event handlers
			//m_PictureBox.Click += new EventHandler(PictureBox_Click);
			//m_PictureBox.SizeChanged += new EventHandler(PictureBox_SizeChanged);
			//ControlPanel.SizeChanged += new EventHandler(ControlPanel_SizeChanged);
		}

		/////////////////////////////////////////////////////////////////////////////
		// protected override methods

		protected override bool SaveFile(string _strFilename)
		{
			// open the file
			FileStream fsave = new FileStream(_strFilename, FileMode.Create);
			BinaryWriter bsave = new BinaryWriter(fsave);

			// write the width, height, and mip count
			bsave.Write((UInt32)m_Image[0].Width);
			bsave.Write((UInt32)m_Image[0].Height);
			bsave.Write((UInt32)m_Image.Length);

			// write the image data
			for (int i = 0; i < m_Image.Length; ++i)
			{
				// write the current mip
				for (int y = 0; y < m_Image[i].Height; ++y)
				{
					for (int x = 0; x < m_Image[i].Width; ++x)
					{
						bsave.Write(m_Image[i].GetPixel(x, y).R);
						bsave.Write(m_Image[i].GetPixel(x, y).G);
						bsave.Write(m_Image[i].GetPixel(x, y).B);
					}
				}
			}

			// close the file
			bsave.Close();
			fsave.Close();

			// success
			return true;
		}

		protected override bool OpenFile(string _strFilename)
		{
			// open the file
			FileStream fopen = new FileStream(_strFilename, FileMode.Open);
			BinaryReader bopen = new BinaryReader(fopen);

			// read the width, height, and mip count
			int iWidth = (int)bopen.ReadUInt32();
			int iHeight = (int)bopen.ReadUInt32();
			int iMipCount = (int)bopen.ReadUInt32();

			// create all mips of the image in memory
			m_Image = new Bitmap[iMipCount];

			// read the image data
			for (int i = 0; i < iMipCount; ++i)
			{
				// create the current mip
				m_Image[i] = new Bitmap(iWidth, iHeight);

				// read the current mip
				for (int y = 0; y < iHeight; ++y)
				{
					for (int x = 0; x < iWidth; ++x)
					{
						byte R = bopen.ReadByte();
						byte G = bopen.ReadByte();
						byte B = bopen.ReadByte();
						m_Image[i].SetPixel(x, y, Color.FromArgb(R, G, B));
					}
				}

				// the next mip is half the size
				iWidth /= 2;
				iHeight /= 2;
			}

			// close the file
			bopen.Close();
			fopen.Close();

			// success
			return true;
		}

		/////////////////////////////////////////////////////////////////////////////
		// event handlers

		private void PictureBox_Click(object sender, EventArgs e)
		{
			// let the user choose a file to open
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "Image Files (*.BMP;*.GIF;*.JPEG;*.PNG;*.TIFF)|*.BMP;*.GIF;*.JPEG;*.PNG;*.TIFF";
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				// create a new bitmap from the file
				Bitmap image = new Bitmap(ofd.FileName);

				// get the width and height
				int iWidth = image.Width;
				int iHeight = image.Height;

				// get the mip count and create a bitmap for each level
				uint uMipCount = 1 + (uint)Math.Floor(Math.Log(Math.Max(iWidth, iHeight), 2));
				m_Image = new Bitmap[uMipCount];

				// render the source image into each mip
				for (uint i = 0; i < uMipCount; ++i)
				{
					// create the mip and render the image into it
					m_Image[i] = new Bitmap(iWidth, iHeight);
					Graphics g = Graphics.FromImage((Image)m_Image[i]);
					g.DrawImage(image, 0, 0, iWidth, iHeight);

					// the next mip is half the size
					iWidth /= 2;
					iHeight /= 2;
				}

				// update the form
				UpdateImage();
			}

			// the file has now changed since the last save
			Dirty = true;
		}

		private void PictureBox_SizeChanged(object sender, EventArgs e)
		{
			CenterImage();
		}

		private void ControlPanel_SizeChanged(object sender, EventArgs e)
		{
			CenterImage();
		}

		/////////////////////////////////////////////////////////////////////////////
		// private methods

		private void UpdateImage()
		{
			//// draw all the mipmaps to the picture box
			//m_PictureBox.Image = new Bitmap(m_Image[0].Width * 2, m_Image[0].Height);
			//Graphics screen = Graphics.FromImage(m_PictureBox.Image);
			//int iX = 0;
			//for (uint i = 0; i < m_Image.Length; ++i)
			//{
			//    screen.DrawImage(m_Image[i], iX, 0);
			//    iX += m_Image[i].Width;
			//}
		}

		private void CenterImage()
		{
			//Point location = new Point();

			//if (m_PictureBox.Width < ControlPanel.Width)
			//{
			//    location.X = (ControlPanel.Width - m_PictureBox.Width) / 2;
			//}
			//else
			//{
			//    location.X = 0;
			//}

			//if (m_PictureBox.Height < ControlPanel.Height)
			//{
			//    location.Y = (ControlPanel.Height - m_PictureBox.Height) / 2;
			//}
			//else
			//{
			//    location.Y = 0;
			//}

			//m_PictureBox.Location = location;
		}

		/////////////////////////////////////////////////////////////////////////////
		// private fields

		private Bitmap[] m_Image;
	}
}
